package com.zed.api.domain;

/**
 * {@link AggregatableAware},聚合感知接口
 *
 * @author Zed
 */
public interface AggregatableAware {

    /**
     * 设置<code>aggregate</code>
     *
     * @param aggregate {@link Aggregatable},聚合体，可能为null.
     */
    void setAggregate(Aggregatable<?> aggregate);

    /**
     * 尝试向<code>target</code>绑定<code>aggregate</code>
     *
     * @param target    {@link T},目标对象
     * @param aggregate {@link Aggregatable},聚合体
     * @return {@link boolean}, 如果<code>target</code>支持聚合，则返回<code>true</code>,否则返回<code>false</code>
     */
    static <T> boolean mayBeToBind(T target, Aggregatable<?> aggregate) {
        if (target == null) return false;

        if (target instanceof AggregatableAware) {
            ((AggregatableAware) target).setAggregate(aggregate);
            return true;
        }

        return false;
    }
}
